#include <glib.h>
#include <gtk/gtk.h>

#include "totem-plugin.h"
#include "totem.h"
#include "totem-lrc.h"
#include <unistd.h>

static
void status_hide_show(GtkStatusIcon *status_icon,GtkWidget *data);

static
void on_destroy(GtkObject *object,
               gpointer   user_data);

static
GtkStatusIcon *totem_lrc_status_icon(GtkWidget *widget,gchar *ico);


static
void on_destroy(GtkObject *object,
               gpointer   user_data)
{
	g_object_unref(user_data);
	lmp_debug("destroy.");
}

static
void status_hide_show(GtkStatusIcon *status_icon,GtkWidget *data)
{

	gtk_window_present(GTK_WINDOW(data));
	g_object_unref(status_icon);
}

static
GtkStatusIcon *totem_lrc_status_icon(GtkWidget *widget,gchar *ico)
{
	GtkStatusIcon* icon;
 
	if(ico&&access(ico,F_OK)==0)
		icon = gtk_status_icon_new_from_file (ico);
	else 
		icon = gtk_status_icon_new_from_stock (GTK_STOCK_YES);
	gtk_status_icon_set_tooltip(icon,_("Show lyric window "));
	g_signal_connect((gpointer)icon, "activate", G_CALLBACK(status_hide_show), widget);
//   g_signal_connect((gpointer)icon, "popup-menu", G_CALLBACK(status_pop_menu), menu);
	return icon;
}

static 
gboolean on_lrcshow_win(GtkWidget *widget,GdkEvent  *event,gpointer data)
{
	lmp_debug("");
	GtkStatusIcon *s_icon = totem_lrc_status_icon(widget,NULL);
	g_signal_connect(widget,"destroy",G_CALLBACK(on_destroy),s_icon);
	gtk_widget_hide(widget);
	return TRUE;
}

GtkWindow *totem_lrc_plugin_get_win(TotemLrcPlugin *pi)
{
	lmp_debug("go");
	GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	gtk_widget_set_size_request(win,-1,450);
///	if(pi->minfo->title)
	gtk_window_set_title(GTK_WINDOW(win),_("Lyric Window"));

//	gtk_widget_set_parent(win,GTK_WIDGET(totem_get_main_window(lrc_totem)));
	gtk_container_add(GTK_CONTAINER(win),pi->pad);
	gtk_window_set_gravity(GTK_WINDOW(win),GDK_GRAVITY_NORTH_EAST);
	gtk_window_set_position(GTK_WINDOW(win),GTK_WIN_POS_CENTER);
	g_signal_connect(win,"delete-event",G_CALLBACK(on_lrcshow_win),NULL);

	return GTK_WINDOW(win);
}
